using Digitalmes.Application.Sys.Dicts;
using Digitalmes.Application.Sys.Dicts.Commands;
using Digitalmes.Application.Sys.Dicts.Queries;

namespace Digitalmes.WebApi.Endpoints.Sys;

/// <summary>
/// 字典数据终结点
/// </summary>
public sealed class DictEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "sys", "dict")
           .RequireAuthorization()
           .MapGet(GetDictPagedListAsync, "list-page")
           .MapGet(GetDictByCodeAsync, "with-code")
           .MapPost(CreateDictAsync)
           .MapPut(UpdateDictAsync, "{id}")
           .MapDelete(DeleteDictAsync, "{id}")
           .MapGet(GetDictItemPagedListAsync, "item/list-page")
           .MapPost(CreateDictItemAsync, "item")
           .MapPut(UpdateDictItemAsync, "item/{id}")
           .MapDelete(DeleteDictItemAsync, "item/{id}");
    }

    [EndpointDescription("字典分页查询")]
    public async Task<IApiResult> GetDictPagedListAsync(ISender sender, [AsParameters] GetDictListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("按字典代码查询字典")]
    public async Task<IApiResult> GetDictByCodeAsync(ISender sender, [AsParameters] GetDictByCodeQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建字典")]
    public async Task<IApiResult> CreateDictAsync(ISender sender, SysDictDto dto)
    {
        var result = await sender.Send(new CreateDictCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新字典信息")]
    public async Task<IApiResult> UpdateDictAsync(ISender sender, long id, SysDictDto dto)
    {
        var result = await sender.Send(new UpdateDictCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除字典")]
    public async Task<IApiResult> DeleteDictAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteDictCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("分页产线字典数据项")]
    public async Task<IApiResult> GetDictItemPagedListAsync(ISender sender, [AsParameters] GetDictItemListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建字典数据项")]
    public async Task<IApiResult> CreateDictItemAsync(ISender sender, SysDictItemDto dto)
    {
        var result = await sender.Send(new CreateDictItemCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新字典数据项")]
    public async Task<IApiResult> UpdateDictItemAsync(ISender sender, long id, SysDictItemDto dto)
    {
        var result = await sender.Send(new UpdateDictItemCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除字典数据项")]
    public async Task<IApiResult> DeleteDictItemAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteDictItemCommand(id));
        return result.ToApiResult();
    }
}
